home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Sample Code / Snippets / QuickDraw / CopybitsSpeedPalette / CopybitsSpeedPalette.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  9.3 KB  |  356 lines  |  [TEXT/MPS ]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        CopybitsSpeedPalette.                                                */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    This program demostrates ways to increase copybits    */
  7. /*            speed when using palettes.                                        */
  8. /*                                                                            */
  9. /*        Version:        1.0 Completed 11/12/91                                */
  10. /*                                                                            */
  11. /*--------------------------------------------------------------------------*/
  12.  
  13. #include     <GestaltEqu.h>
  14. #include    <Palette.h>
  15. #include    <QDOffscreen.h>
  16.  
  17. #define Gestalttest        0xA1AD
  18. #define NoTrap            0xA89F
  19.  
  20. #define    appleID            128            
  21. #define    appleMenu        0
  22. #define    aboutMeCommand    1
  23.  
  24. #define    fileID            129
  25. #define    quitCommand     1
  26.  
  27. #define    PictID            128
  28. #define    clutID            150
  29.  
  30. #define    aboutMeDLOG        128
  31. #define    okButton        1
  32.  
  33. /*------------------------------------------------------*/
  34. /*    Global Variables.                                    */
  35. /*------------------------------------------------------*/
  36.  
  37. Rect                TotalRect, minRect, WinMinusScroll, InitWindowSize;
  38. WindowPtr            myWindow;
  39. CTabHandle            mycolors;
  40. PaletteHandle        srcPalette;
  41. Boolean                DoneFlag;
  42. MenuHandle            mymenu0, mymenu1;
  43. PicHandle            ThePict;
  44. GWorldPtr            offscreenGWorld;
  45.  
  46. /*------------------------------------------------------*/
  47. /*    showAboutMeDialog().                                */
  48. /*------------------------------------------------------*/
  49.  
  50. void showAboutMeDialog()
  51. {
  52.     GrafPtr     savePort;
  53.     DialogPtr    theDialog;
  54.     short        itemHit;
  55.  
  56.     GetPort(&savePort);
  57.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  58.     SetPort(theDialog);
  59.  
  60.     do {
  61.         ModalDialog(nil, &itemHit);
  62.     } while (itemHit != okButton);
  63.  
  64.     CloseDialog(theDialog);
  65.  
  66.     SetPort(savePort);
  67.     return;
  68. }
  69.  
  70. /*------------------------------------------------------*/
  71. /*    init().                                                */
  72. /*------------------------------------------------------*/
  73.  
  74. void init()
  75. {
  76.     RgnHandle            tempRgn;
  77.     Rect                BaseRect;
  78.     OSErr                err;
  79.     long                QDfeature, OSfeature;
  80.     GDHandle            SaveGD;
  81.     CGrafPtr            SavePort;
  82.  
  83.     /*    Initialize Managaer.    */
  84.     InitGraf(&qd.thePort);
  85.     FlushEvents(everyEvent, 0);
  86.     InitWindows();
  87.     InitDialogs(nil);
  88.     InitCursor();
  89.  
  90.     /*    Set up menus.    */
  91.     mymenu0 = GetMenu(appleID);
  92.     AddResMenu(mymenu0, 'DRVR');
  93.     InsertMenu(mymenu0,0);
  94.     mymenu1 = GetMenu(fileID);
  95.     InsertMenu(mymenu1,0);
  96.     DrawMenuBar();
  97.     DoneFlag = FALSE;
  98.     ThePict = GetPicture(PictID);
  99.     if (ThePict == nil)
  100.         DoneFlag = TRUE;
  101.  
  102.     /*    Use Gestalt to find is QuickDraw is avaiable.    */
  103.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  104.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  105.         if (err)
  106.             DoneFlag = TRUE;
  107.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  108.         if (err)
  109.             DoneFlag = TRUE;
  110.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0605)
  111.             DoneFlag = TRUE;
  112.         }
  113.     else
  114.         DoneFlag = TRUE;
  115.  
  116.     /*    Set Rects.    */
  117.     SetRect(&BaseRect, 40, 60, 472, 282);
  118.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  119.                 BaseRect.bottom - 80);
  120.     SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top, 
  121.                             WinMinusScroll.right, WinMinusScroll.bottom);
  122.     tempRgn = GetGrayRgn();
  123.     HLock ((Handle) tempRgn);
  124.     TotalRect = (**tempRgn).rgnBBox;
  125.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  126.                 (**tempRgn).rgnBBox.bottom - 40);
  127.     HUnlock ((Handle) tempRgn);
  128.  
  129.     /*    Open window and set up picture.    */
  130.     GetGWorld (&SavePort, &SaveGD);
  131.     mycolors = GetCTable (clutID);
  132.     (*mycolors)->ctFlags |= 0x4000;
  133.  
  134.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  135.                             (WindowPtr) -1, TRUE, 150);
  136.     SetGWorld((CGrafPtr)myWindow, SaveGD);
  137.     DrawGrowIcon (myWindow);
  138.  
  139.     srcPalette = NewPalette (((**mycolors).ctSize)+1, mycolors,
  140.             pmTolerant + pmExplicit + pmAnimated, 0);
  141.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  142.     
  143.     GetGWorld (&SavePort, &SaveGD);
  144.     err = NewGWorld (&offscreenGWorld, 8, &InitWindowSize, mycolors, nil, 0);
  145.     if (err)
  146.         Debugger();
  147.     SetGWorld (offscreenGWorld, nil);
  148.     EraseRect (&InitWindowSize);
  149.     DrawPicture (ThePict, &InitWindowSize);
  150.     SetGWorld (SavePort, SaveGD);
  151. }
  152.  
  153. /*------------------------------------------------------*/
  154. /*    doCommand().                                        */
  155. /*------------------------------------------------------*/
  156.  
  157. void doCommand(mResult)
  158.     long    mResult;
  159. {
  160.     int                     theMenu, theItem;
  161.     char                    daName[256];
  162.     GrafPtr                 savePort;
  163.  
  164.     theItem = LoWord(mResult);
  165.     theMenu = HiWord(mResult);
  166.     
  167.     switch (theMenu) {
  168.         case appleID:
  169.             if (theItem == aboutMeCommand)
  170.                 showAboutMeDialog();
  171.             else {
  172.                 GetItem(mymenu0, theItem, daName);
  173.                 GetPort(&savePort);
  174.                 (void) OpenDeskAcc(daName);
  175.                 SetPort(savePort);
  176.             }
  177.             break;
  178.  
  179.         case fileID:
  180.             switch (theItem) {
  181.                 case quitCommand:
  182.                     DoneFlag = TRUE;
  183.                     break;
  184.                 default:
  185.                     break;
  186.                 }
  187.             break;
  188.     }
  189.     HiliteMenu(0);
  190.     return;
  191. }
  192.  
  193. /*------------------------------------------------------*/
  194. /*    draw.                                                */
  195. /*------------------------------------------------------*/
  196.  
  197. void draw()
  198. {
  199.     RGBColor        black = {0,0,0}, White = {65535,65535,65535};
  200.     long            before, delay;
  201.     char theString[255];
  202.     GDHandle        screensDevice;
  203.     Rect            area;
  204.         
  205.     RGBForeColor (&black);
  206.     RGBBackColor (&White);
  207.     EraseRect(&WinMinusScroll);
  208.     
  209.     /* This is the only change made to support a faster copybits on one screen.
  210.         ctFlags is still set above. */
  211.     area = WinMinusScroll;
  212.     LocalToGlobal(&topLeft(area));
  213.     LocalToGlobal(&botRight(area));
  214.     screensDevice = GetMaxDevice(&area);
  215.     if (screensDevice != nil)
  216.         (**(**offscreenGWorld->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  217.         
  218.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  219.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  220.     EraseRect(&WinMinusScroll);
  221.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  222.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  223.     EraseRect(&WinMinusScroll);
  224.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  225.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  226.     EraseRect(&WinMinusScroll);
  227.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  228.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  229.     EraseRect(&WinMinusScroll);
  230.     before = TickCount();
  231.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  232.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  233.     delay = TickCount() - before;
  234. /*    DebugStr("\PIt's me!!!");    */
  235. }
  236.  
  237. /*------------------------------------------------------*/
  238. /*    main().                                                */
  239. /*------------------------------------------------------*/
  240.  
  241. main()
  242. {
  243.     char            key;
  244.     Boolean            track;
  245.     long            growResult;
  246.     EventRecord     myEvent;
  247.     WindowPtr        whichWindow;
  248.     int                yieldTime;
  249.  
  250.  
  251.     init();
  252.     yieldTime = 0;
  253.     for ( ;; ) {
  254.         if (DoneFlag)
  255.             ExitToShell();
  256.             
  257.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  258.             switch (myEvent.what) {
  259.                 case mouseDown:
  260.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  261.                         case inSysWindow:
  262.                             SystemClick(&myEvent, whichWindow);
  263.                             break;
  264.                         case inMenuBar:
  265.                             doCommand(MenuSelect(myEvent.where));
  266.                             break;
  267.                         case inContent:
  268.                             break;
  269.                         case inDrag:
  270.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  271.                             draw();
  272.                             DrawGrowIcon (whichWindow);
  273.                             break;
  274.                         case inGrow:
  275.                             growResult = GrowWindow (whichWindow, myEvent.where,
  276.                                                     &minRect);
  277.                             SizeWindow(whichWindow, LoWord(growResult), 
  278.                                     HiWord(growResult), TRUE);
  279.                             EraseRect(&whichWindow->portRect);
  280.                             SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  281.                                     whichWindow->portRect.top, 
  282.                                     whichWindow->portRect.right-20, 
  283.                                     whichWindow->portRect.bottom - 20);
  284.                             draw();
  285.                             DrawGrowIcon (whichWindow);
  286.                             break;
  287.                         case inGoAway:
  288.                             track = TrackGoAway (whichWindow, myEvent.where);
  289.                             if (track) {
  290.                                 CloseWindow (whichWindow);
  291.                                 DoneFlag = TRUE;
  292.                                 }
  293.                             break;
  294.                         case inZoomIn:
  295.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  296.                             if (track) {
  297.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  298.                                 EraseRect(&whichWindow->portRect);
  299.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  300.                                         whichWindow->portRect.top, 
  301.                                         whichWindow->portRect.right-20, 
  302.                                         whichWindow->portRect.bottom - 20);
  303.                                 draw();
  304.                                 DrawGrowIcon (whichWindow);
  305.                                 }
  306.                             break;
  307.                         case inZoomOut:
  308.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  309.                             if (track) {
  310.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  311.                                 EraseRect(&whichWindow->portRect);
  312.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  313.                                         whichWindow->portRect.top, 
  314.                                         whichWindow->portRect.right-20, 
  315.                                         whichWindow->portRect.bottom - 20);
  316.                                 draw();
  317.                                 DrawGrowIcon (whichWindow);
  318.                                 }
  319.                             break;
  320.                         default:
  321.                             break;
  322.                         }
  323.                     break;
  324.                 case keyDown:
  325.                 case autoKey:
  326.                     key = myEvent.message & charCodeMask;
  327.                     if ( myEvent.modifiers & cmdKey )
  328.                         if ( myEvent.what == keyDown )
  329.                             doCommand(MenuKey(key));
  330.                     break;
  331.                 case updateEvt:
  332.                     if ((WindowPtr) myEvent.message == myWindow) {
  333.                         BeginUpdate((WindowPtr) myWindow);
  334.                         EndUpdate((WindowPtr) myWindow);
  335.                         draw();
  336.                         }
  337.                     break;
  338.                 case diskEvt:
  339.                     break;
  340.                 case activateEvt:
  341.                     break;
  342.                 case app4Evt:
  343.                     if ((myEvent.message << 31) == 0) {
  344.                         yieldTime = 30;
  345.                         }
  346.                     else {
  347.                         yieldTime = 0;
  348.                         SetPort((WindowPtr) myWindow);
  349.                         }
  350.                     break; 
  351.                 default:
  352.                     break;
  353.                 }
  354.             }
  355.         }
  356. }